﻿using System;
using System.Linq;
using System.Web.Mvc;
using Contacts.Models;
using System.Collections.Generic;

namespace Contacts.Controllers {
  public class ContactsController : Controller {
    private Database db = new Database();

    [HttpGet]
    public ActionResult Index() {
      return View(db.Contacts);
    }

    [HttpGet]
    public ActionResult Details(int id) {
      return View(db.Contacts.SingleOrDefault(c => c.ContactId == id));
    }

    [HttpGet]
    public ActionResult Create() {
      return View(new Contact());
    }

    [HttpPost]
    public ActionResult Create(Contact contact) {
      db.Contacts.Add(contact);
      db.SaveChanges();
      return RedirectToAction("Index");
    }

    [HttpGet]
    public ActionResult Edit(int id) {
      var contact = db.Contacts.SingleOrDefault(c => c.ContactId == id);
      return View(contact);
    }

    [HttpPost]
    public ActionResult Edit(int id, Contact form) {
      var contact = db.Contacts.SingleOrDefault(c => c.ContactId == id);
      UpdateModel(contact);
      db.SaveChanges();
      return RedirectToAction("Index");
    }
  }
}
